/**
 * Applikationsutveckling i Java, 5DV135
 * HT12
 * Project
 *
 * File: Tower.java
 * Description: An abstract class representing a shooting tower in a game
 * Date: 18 december 2012
 *
 * cs-user: dv11mgr
 * @author Maria Gramner
 */
package nullInvader.Engine;
import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;


abstract public class Tower implements Field {

    private int damage;
    private ArrayList<Zone> reachableZones;
    private int range;
    private Unit currentTarget;
    private boolean movable;
    private Point ownPosition;


    /**
     * Constructor. Sets the movability, damage and range of the tower
     * @param movable boolean.
     * @param damage Integer. Note! the value must be negative, otherwise
     * the shot will increase the victims health rather than decrease it.
     * @param range Integer. How far the tower will reach.
     */
    public Tower(boolean movable, int damage, int range, Point position){
        this.movable = movable;
        this.damage = damage;
        this.range = range;
        ownPosition = position;
        setIcon();
        currentTarget = null;
    }


    /**
     * Method to set the list of the zones the tower can reach
     * @param neighbours ArrayList<Zone>.
     */
    public void setReachableZones(ArrayList<Zone> neighbours) {
        reachableZones = neighbours;
    }


    /**
     * Method to get the list of the zones reachable from the tower
     * @return ArrayList<Zone>
     */
    public ArrayList<Zone> getReachableZones ( ) {
        return reachableZones;
    }


    /**
     * Method to get the range of the tower
     * @return Integer
     */
    public int getRange() {
        return range;
    }


    /**
     * Method to make the tower shoot at a target in the
     * reachable zones.
     */
    public void takeShot(){
        /*Reset currentTarget*/
        currentTarget = null;
        /*Search the reachable zones for a target*/
        for(int i=0; currentTarget==null && i<reachableZones.size(); i++){
            /*If the zone contains units, pick the first one as target*/
            if(!reachableZones.get(i).getUnitList().isEmpty()){
                currentTarget = reachableZones.get(i).getUnitList().get(0);
            }
        }
        /*If a target was found, modify the health of that unit*/
        if(currentTarget!=null){
            currentTarget.modifyHealth(damage);
        }
    }


    /**
     * Method to check if it is possible to move on the object
     * @return boolean
     */
    public boolean isMovable () {
        return movable;
    }


    /**
     * Abstract method to get the image for the tower
     * @return Image
     */
    public abstract Image getIcon();


    /**
     * Abstract method to set the correct image on the class
     * extending Tower
     */
    protected abstract void setIcon();


    /**
     * Abstract method for setting an effect on the unit
     * currently on the tower
     * @param unit
     */
    public abstract void landOn(Unit unit);


    /**
     * Abstract method for a possible effect that may occur when the tower
     * is being clicked on
     */
    public abstract void wasClicked();



    /**
     * Method for retrieving the last Unit that was shot by this tower.
     * @return Unit
     */
    public Unit getCurrentTarget(){
        return currentTarget;
    }


    /**
     * Method for getting the position of the tower
     * @return Point
     */
    public Point getPosition(){
        return ownPosition;
    }
}
